|
Concepts and the related notion of axioms were an extension to C++'s template system proposed for C++11. They were designed to improve compiler diagnostics and to allow programmers to codify in the program some formal properties of templates that they write. Incorporating these limited formal specifications into the program (in addition to improving code clarity) can guide some compiler optimizations, and can potentially help improve program reliability through the use of formal verification tools to check that the implementation and specification actually match. In July 2009, the C++11 committee decided to remove concepts from the draft standard, as they were considered "not ready" for C++11.〔(InformIT: The Removal of Concepts From C++11 )〕〔“At this meeting (Committee, Frankfurt, July 2009 ), Concepts, the major feature of C++ 0x, which enables constrained genericity, or template argument prototyping, has been removed from the C++0x draft.” (Michael Wong ), the IBM and Canadian representative to the C++ Standard and OpenMP Committee. (''The View (or trip report) from the July 2009 C++ Standard meeting'' )〕 There are unofficial plans to add concepts back in a future version of the standard in some form,〔Michael Wong: "I am sure this will not be the last we will hear of it." () Bjarne Stroustrup: "all expressed support for "concepts," just "later" and "eventually." ... I hope we will see "concepts" in a revision of C++ in maybe five years." () Beman Dawes: "The committee is very much in favor of Concepts ... Committee members seem to lean toward letting the two rival Concept teams go back to work outside of the committee process. The expectation being they will come back to the committee when they are ready to restart the standardization process."()〕 but no official decision has been made yet. This article documents concepts as they last appeared in a published working paper.〔(【引用サイトリンク】 title=N2914 "Working Draft, Standard for Programming Language C++" )〕 A preliminary version of concepts for C++ was implemented as ConceptGCC, and will be made available in GCC 6. There are plans for a form of concepts to be introduced into C++ in a cut down form called Concepts Lite (it checks function arguments, but not their usage), with plans to extend it to be fully functional concepts at a later date (using the revised and new syntax, without requiring the C++0x style of concept definition).〔http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3701.pdf〕 ==Motivation== C++ class templates and function templates must impose restrictions on the types that they take. For instance, the standard library containers require that the contained types be assignable. Unlike the dynamic polymorphism that class inheritance hierarchies exhibit, where a function that accepts an object of type can be passed any subtype of , for templates any class can be supplied as a template parameter so long as it supports all of the operations that users of actual instantiations of that type use. In the case of polymorphism, the requirement an argument must meet is clear (being a subtype of ), but in the case of a template the interface an object must meet is implicit in the implementation of that template. Concepts provide a mechanism for codifying the interface that a template parameter must meet. The primary motivation of the introduction of concepts is to improve the quality of compiler error messages. If a programmer attempts to use a type that does not provide the interface a template requires, the compiler will generate an error. However, such errors are often difficult to understand, especially for less experienced programmers. There are two main reasons for this. First, error messages are often displayed with template parameters spelled out in full; this leads to extremely large error messages. On some compilers, simple errors can generate several kilobytes of error messages. Second, they often do not immediately refer to the actual location of the error. For example, if the programmer tries to construct a of objects that do not have a copy constructor, the first error almost always refers to the code within the class itself that attempts to copy construct its contents; the programmer must be skilled enough to understand that the real error was that the type doesn't support everything the class requires. In an attempt to resolve this issue, it was proposed that C++11 add the language feature of ''concepts''. Similar to how object-oriented programming (OOP) uses a base class to define restrictions on what a type can do, a concept is a named construct that specifies what a type must provide. Unlike OOP, however, the concept definition itself is not always associated explicitly with the type being passed into the template, but with the template definition itself: template const T& min(const T &x, const T &y) Rather than using an arbitrary or for the template type parameter, it uses , which is a concept that was previously defined. If a type passed into the function template does not satisfy the requirements of the concept, then a compile error will result, telling the user that the type used to instantiate the template does not fit the concept. A more generalized form of the concept is as follows: template const T& min(const T &x, const T &y) The keyword begins a list of ''requirements'', which are specified by one or more concepts. In the requirement list, multiple concepts can be combined with logical operators like those of negation (!) and logical conjunction (&&) in order to form template constraints. For example, a user may prevent the use of types meeting the requirements of the concept '' by using the syntax '.' This mechanism can be used in a way similar to template specialization. A general template would handle types with fewer features, explicitly disallowing the use of other, more feature-rich, concepts. Those concepts, in turn, might have their own specializations that could use those particular features to achieve greater performance or some other functionality. The operation && of logical conjunction is also easy to use in the requirement list. For example, a user could specify that a template requires the objects passed to it to be of types having both assignment and copy construction simply by adding to such a template's declaration and/or definition. 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「Concepts (C++)」の詳細全文を読む スポンサード リンク
|